﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BST250301TestStand.Models.Utils
{
    internal class PLCControl
    {
        public static async Task<bool> StartEngineReady()
        {
            //if (!PLCConfig.SetPLCSpeed(5))//先发0转速
            //{
            //    return false;
            //}
            if (!PLCConfig.GetPLCAuto())
            {
                return false;
            }
            if (!PLCConfig.GetPLCSafeStop())
            {
                return false;
            }
            return true;
            ////if (!PLCConfig.SetPLCDriverConnect(true))
            ////{
            ////    return false;
            ////}

            ////bool isC = false;
            ////await Task.Run(async () =>
            ////{
            ////    int waitS = 30;
            ////    while (waitS > 0)
            ////    {
            ////        waitS--;
            ////        await Task.Delay(1000);
            ////        if (PLCConfig.GetPLCDriverConnected())
            ////        {
            ////            isC = true;
            ////            break;
            ////        }
            ////    }
            ////});
            ////if (!isC)
            ////    return false;
            ////await Task.Delay(500);

           // if (!PLCConfig.SetPLCDriverStart(true))
           //     return false;
           //bool isC = false;
           // await Task.Run(async () =>
           // {
           //     int waitS = 20;
           //     while (waitS > 0)
           //     {
           //         waitS--;
           //         await Task.Delay(500);
           //         if (PLCConfig.GetPLCDriverSuccess())
           //         {
           //             isC = true;
           //             break;
           //         }
           //     }
           // });
           // return isC;
        }
      
        /// <summary>
        /// 停止驱动端，未加气缸
        /// </summary>
        /// <returns></returns>
        public static async Task<bool> StopEngine()
        {
            if (!PLCConfig.GetPLCAuto())
            {
                return false;
            }
            if (!PLCConfig.GetPLCSafeStop())
            {
                return false;
            }

            //bool isT = false;
            if (! await PLCConfig.SetPLCSpeed(0,true))
                return false;

            //if (PLCConfig.GetPLCDriverSpeed() >= 1)
            //{
            //    await Task.Run(async () =>
            //    {
            //        int waitS = 10;
            //        while (waitS > 0)
            //        {
            //            waitS--;
            //            await Task.Delay(1000);
            //            if (PLCConfig.GetPLCDriverSpeed() <= 1)
            //            {
            //                isT = true;
            //                break;
            //            }
            //        }
            //    });
            //}
            //else
            //    isT = true;
            //await Task.Delay(1000);
            //if (!isT)
            //    return false;


            if (!PLCConfig.SetPLCDriverStart(false))
                return false;

            await Task.Delay(1000);
            if (!PLCConfig.GetPLCDriverSuccess())
                return true;
            else
                return false;
            //if (!PLCConfig.SetPLCDriverDisconnect(true))
            //    return false;

            //bool isO = false;
            //if (!PLCConfig.GetPLCDriverDisconnected())
            //{
            //    await Task.Run(async () =>
            //    {
            //        int waitS = 10;
            //        while (waitS > 0)
            //        {
            //            waitS--;
            //            await Task.Delay(1000);
            //            if (PLCConfig.GetPLCDriverDisconnected())
            //            {
            //                isO = true;
            //                break;
            //            }
            //        }
            //    });
            //}

            //await Task.Delay(1000);

            //return isO;
            //return true;
        }
    }
}
